package com.orm.analysis.action;

import com.opensymphony.xwork2.ActionContext;
import com.orm.analysis.model.Address;
import com.orm.analysis.model.ContactInfo;
import com.orm.analysis.model.Item;
import com.orm.analysis.model.Supplier;
import com.orm.analysis.service.GenericService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class ItemAction extends FrontendAction {

    private GenericService<Item> service;
    private GenericService<Supplier> supplierService;
    private Item item;
    private List<Item> items;
    private List<Supplier> suppliers;
    private Integer[] supplierIdList;


    public ItemAction(GenericService<Item> service, GenericService<Supplier> supplierService) {
        this.service = service;
        this.supplierService = supplierService;
    }

    public String getAllItems() {
//        items = service.findAllWithDepth("suppliers");
        items = service.list(page, size);
        return SUCCESS;
    }


    public String itemBulkTask() {
           try {
               service.executeImproter();
           } catch (Exception e) {
               e.printStackTrace();
           }
           return SUCCESS;
       }



    public String getBulkItems() {
              try {
                  items = service.findAllWithDepth("suppliers");

              } catch (Exception e) {
                  e.printStackTrace();
              }
              return SUCCESS;
          }

    public String getAllItemSuppliers() {
              try {
                  items = service.findAllWithDepth("suppliers");

              } catch (Exception e) {
                  e.printStackTrace();
              }
              return SUCCESS;
          }


    public String list() {
        items = service.list(page, size);
        return SUCCESS;
    }

    public String setUpForInsert() {
        prep();

        return "success";
    }
    public String setUpForUpdate() {
        prep();
        if (item != null && item.getItemId() != null) {
            item = service.getById(item.getItemId());
        }
        return "success";
    }


    public String insert() {

        prep();

        if (!validationSuccessful()) {
            return "input";
        } else {


            if (item.getItemId() == null) {
                service.create(item);

                if (item.getSupplierIdList() != null && item.getSupplierIdList().length > 0) {
                    item.setSuppliers(supplierService.getByIdList(item.getSupplierIdList()));
                }

                service.update(item);

            }

        }
        return "success";
    }


    public String update() {

        prep();

        if (!validationSuccessful()) {
            return "input";
        } else {



                if (item.getSupplierIdList() != null && item.getSupplierIdList().length > 0) {
                    item.setSuppliers(supplierService.getByIdList(item.getSupplierIdList()));
                }
                service.update(item);


        }
        return "success";
    }


    public String getSingleItem() {


//        item = service.findWithDepth("itemId",item.getItemId());
        item = service.findWithDepth("itemId",new Integer(1));
        items = new ArrayList<Item>();
        items.add(item);

           return SUCCESS;
       }
       public String getSingleItemTwice() {


//           item = service.findWithDepth("itemId",item.getItemId());
           item = service.findWithDepth("itemId",new Integer(1));
           item = service.findWithDepth("itemId",new Integer(1));
           items = new ArrayList<Item>();
                  items.add(item);

           return SUCCESS;
       }

    public String getCRUD() {


               if (!validationSuccessful()) {
                           return "input";
                       } else {
                           if (item.getItemId() == null) {
                               item  = service.create(item);
                               item =service.getById(item);
                               item.setItemName("Cements L and T");
                               item =service.update(item);
                               service.delete(item.getItemId());

                           }

                       }



               return SUCCESS;
           }




    public String getBulkUpdateItems() {

            int count= service.updateExecuteUpdateByQuery("UPDATE Item a SET a.price = a.price *1.1");

                Map session = ActionContext.getContext().getSession();
                session.put("bulkUpdateItems", new Integer(count));

            return SUCCESS;
    }
  public String getBulkDeleteItems() {

            int count = service.updateExecuteUpdateByQuery("DELETE FROM Item a where a.quantityInStock < 100");

                Map session = ActionContext.getContext().getSession();
                session.put("bulkDeleteItems", new Integer(count));

            return SUCCESS;
    }

    private void prep() {


        List<Integer> supplierIdArrayList = supplierService.getIdList();
        Map session = ActionContext.getContext().getSession();
        if (supplierIdArrayList != null && supplierIdArrayList.size() > 0) {
            session.put("supplierIdList", supplierIdArrayList.toArray());
        } else {
            session.put("supplierIdList", "");
        }

    }



    public String delete() {
        service.delete(item.getItemId());
        return SUCCESS;
    }


    private boolean validationSuccessful() {
        if (item.getItemName() == null || item.getItemName().trim().length() < 1) {
            addActionMessage("ItemName is required");
        }
        if (item.getPrice() == null || item.getPrice() < 0) {
            addActionMessage("Price is required");
        }

        if (this.hasActionMessages()) {
            return false;
        } else {
            return true;
        }


    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public List<Supplier> getSuppliers() {
        return suppliers;
    }

    public void setSuppliers(List<Supplier> suppliers) {
        this.suppliers = suppliers;
    }


}
